package com.example.events;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class UserService implements ApplicationListener<UserEvent> {
    @Override
    public void onApplicationEvent(UserEvent userEvent) {
        if (userEvent instanceof UserLoginEvent loginEvent) {
            System.out.println("用户登录事件处理：" + loginEvent);
            return;
        }

        if (userEvent instanceof  UserRegisterEvent userRegisterEvent) {
            System.out.println("用户注册事件处理：" + userRegisterEvent);
            return;
        }

    }

    // 注解方式处理事件监听
    @EventListener
    public void handleUserLoginEvent(UserLoginEvent loginEvent) {
        System.out.println("annotation 用户登录事件处理：" + loginEvent);
    }

    @EventListener
    public void handleUserRegisterEvent(UserRegisterEvent registerEvent) {
        System.out.println("annotation 用户注册事件处理：" + registerEvent);
    }
}
